﻿using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;
using ShaderEffectsLibrary;
using System.Windows;
using System;

namespace Behavior
{
    /// <summary>
    /// 我想当鼠标在该元素上的时候添加一个放大镜和动画效果，
    /// 当鼠标离开就删除这些效果作用后的元素范围。 
    /// 我想要处理 MouseEnter、 MouseLeave 事件命令启用和禁用效果。
    /// 对 OnAttached 方法中的前一个实例,我将附加到与性能对象关联的 MouseEnter、 MouseLeave 事件中。 
    /// 并分别在 OnDetaching 方法中我会结束该效果。 鼠标在元素上时我想要跟踪鼠标移动的活动。 
    /// 这就是为什么我会附加鼠标移动事件，
    /// 并会当它离开时结束元素效果。 
    /// </summary>
    public class 放大镜 : System.Windows.Interactivity.Behavior<FrameworkElement>
    {

        private Magnifier magnifier;
        private double originalMagnification;

        public 放大镜() :
            base()
        {
            this.magnifier = new Magnifier();
        }

        /// <summary>
        /// 附加到对像时发生
        /// </summary>
        protected override void OnAttached()
        {
            base.OnAttached();

            AssociatedObject.MouseEnter += new MouseEventHandler(AssociatedObject_MouseEnter);
            AssociatedObject.MouseLeave += new MouseEventHandler(AssociatedObject_MouseLeave);
        }

        void AssociatedObject_MouseLeave(object sender, MouseEventArgs e)
        {
            AssociatedObject.MouseMove -= new MouseEventHandler(AssociatedObject_MouseMove);
            AssociatedObject.Effect = null;
        }

        void AssociatedObject_MouseMove(object sender, MouseEventArgs e)
        {
            (this.AssociatedObject.Effect as Magnifier).Center =
               e.GetPosition(this.AssociatedObject);

            Point mousePosition = e.GetPosition(this.AssociatedObject);
            mousePosition.X /= this.AssociatedObject.ActualWidth;
            mousePosition.Y /= this.AssociatedObject.ActualHeight;
            this.magnifier.Center = mousePosition;
            Storyboard zoomInStoryboard = new Storyboard();
            DoubleAnimation zoomInAnimation = new DoubleAnimation();
            zoomInAnimation.To = this.magnifier.Magnification;
            zoomInAnimation.Duration = TimeSpan.FromSeconds(0.5);
            Storyboard.SetTarget(zoomInAnimation, this.AssociatedObject.Effect);
            Storyboard.SetTargetProperty(zoomInAnimation, new PropertyPath(Magnifier.MagnificationProperty));
            zoomInAnimation.FillBehavior = FillBehavior.HoldEnd;
            zoomInStoryboard.Children.Add(zoomInAnimation);
            zoomInStoryboard.Begin();

        }

        void AssociatedObject_MouseEnter(object sender, MouseEventArgs e)
        {
            AssociatedObject.MouseMove += new MouseEventHandler(AssociatedObject_MouseMove);
            this.AssociatedObject.MouseMove += new MouseEventHandler(AssociatedObject_MouseMove);
            this.AssociatedObject.Effect = magnifier;
        }
        /// <summary>
        /// 与对像分离时发生
        /// </summary>
        protected override void OnDetaching()
        {
            AssociatedObject.MouseEnter -= new MouseEventHandler(AssociatedObject_MouseEnter);
            AssociatedObject.MouseLeave -= new MouseEventHandler(AssociatedObject_MouseLeave);
        }
    }
}
